#include "key.h"

typedef enum
{
  Key_Check = 0,
  Key_PressComfirm = 1,  
  Key_Released = 2 
}KeyState;

typedef enum
{
  NULL_Key, 
  Short_Press,
  Long_Press
}Key_Flag;

uint8_t KeyState_Flag = NULL_Key;
uint8_t KeyCurrentState = Key_Check;

uint8_t Key_Scan(void)
{  
  uint8_t Key_Level = IO_READ();
  static int Key_PressTime = 0;
  static uint8_t Lock = 0;
  
  switch(KeyCurrentState)
  {
    case Key_Check:
      KeyState_Flag  = NULL_Key;
      if(Key_Level == 0)
      {
        KeyCurrentState = Key_PressComfirm;
      }
      Key_PressTime = 0;
      Lock = 0;
      break;
    case Key_PressComfirm:
      if(Key_Level == 0)
      {
        if(Lock == 0)Lock = 1;
        Key_PressTime++;
      }
      else
      {
        if(Lock)
        {
          if(Key_PressTime > 100)
          {
            KeyState_Flag = Long_Press;
          }
          else
          {
            KeyState_Flag = Short_Press;
          }
          KeyCurrentState = Key_Released;
        }
        else
        {
          KeyCurrentState = Key_Check;
        }
      }
      break;
    case Key_Released:
      KeyCurrentState = Key_Check;
      break;
  }
  return KeyState_Flag;
}